package com.wandian.cqcsk.mvvm.mine.mywallet.txchoose;

import android.app.Activity;
import android.app.Application;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.makeramen.roundedimageview.RoundedImageView;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.bean.OSSDataBean;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.FileUtil;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.OssManager;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.cqcsk.bean.MySuggestBean;
import com.wandian.cqcsk.bean.TXConfigBean;
import com.wandian.cqcsk.data.BaseAppApi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;
import static com.mylib.libcore.utils.JsonUtil.fromJson;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description VipCenterViewModel
 **/
public class TXChooseViewModel extends BaseViewModel {

    private BaseAppApi baseAppApi;
    public String picPath="";
    private boolean isApply =false;//是否已申请
    public MutableLiveData<UserInfoBean> userBean = new MutableLiveData<>();
    public MutableLiveData<OSSDataBean> oosBean = new MutableLiveData<>();
    public MutableLiveData<List<TXConfigBean>> listConfig = new MutableLiveData<>();
    public TXChooseViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }

    //获取用户信息
    public void getUserInfo(){
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.getUserInfo(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("getUserInfo",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            try {
                                if(!UIUtils.isEmpty(bean.getData())){
                                    Log.d("crc_rc4key_getVersion", AESUtils.getInstance().decrypt(bean.getData()));
                                    UserInfoBean userInfoBean = JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),UserInfoBean.class);
                                    if(!UIUtils.isEmpty(userInfoBean)){
                                        userBean.setValue(userInfoBean);
                                        AppContext.getInstance().getAppPreferencesHelper().setUserInfoBean(userInfoBean);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    //提现配置
    public void getAmountRatio(){
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.amountRatio(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("amountRatio",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            try {
                                if(!UIUtils.isEmpty(bean.getData())){
                                    List<TXConfigBean> listBean = JsonUtil.fromJson(AESUtils.getInstance().decrypt(bean.getData()), new TypeToken<ArrayList<TXConfigBean>>() {});
                                     if(!UIUtils.isEmpty(listBean) && listBean.size()>0){
                                         listConfig.setValue(listBean);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    //申请提现
    public void applyWithdrawal(String amount, String picPath, TextView tvBtn){
        if(isApply){
            return;
        }
        if(UIUtils.isEmpty(amount)){
            ToastUtil.show("数据异常");
            tvBtn.setVisibility(View.VISIBLE);
            return;
        }
        if(UIUtils.isEmpty(picPath)){
            tvBtn.setVisibility(View.VISIBLE);
            ToastUtil.show("请上传收款码");
            return;
        }
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("amount",Integer.parseInt(amount)+"");
        hashMap.put("payeeCode",picPath);
        hashMap.put("channelCode","wechat");
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.applyWithdrawal(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("applyWithdrawal",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            ToastUtil.show("已申请，注意查收");
                            isApply = true;
                            tvBtn.setVisibility(View.GONE);
                            getUserInfo();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        tvBtn.setVisibility(View.VISIBLE);
                        dismissDialog();
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    //获取oosToken
    public void getOOSToken(){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.getOOSToken(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("getOOSToken",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            String json = AESUtils.getInstance().decrypt(bean.getData())+"";
                            Log.d("getOOSToken",   json);
                            if(!UIUtils.isEmpty(json) && json.length()>4){
                                OSSDataBean ossDataBean =JsonUtil.parseJsonToBean(json,OSSDataBean.class);
                                oosBean.setValue(ossDataBean);
                            }else {
                                ToastUtil.show("网络异常请重试");
                                oosBean.setValue(null);
                            }
                        }else {
                            oosBean.setValue(null);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        oosBean.setValue(null);
                        dismissDialog();
                        Log.d("getOOSToken",   JsonUtil.toJson(e));
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    // 上传图片
    public void upLoadImg(Activity activity, OSSDataBean ossDataBean, ImageView ivPic, String imgPath){
        showDialog();
        try {
            OssManager.getInstance().upload(activity, ossDataBean,imgPath,OssManager.getSKMPicPath(imgPath), 0, new OssManager.OnUploadListener() {

                        @Override
                        public void onProgress(int position, long currentSize, long totalSize) {
                        }

                        @Override
                        public void onSuccess(int position, String uploadPath, String imageUrl) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dismissDialog();
                                    picPath = imageUrl;
                                    GlideUtil.intoImg(activity,picPath,ivPic);
                                }
                            });

                            FileUtil.deleteFileByPath(imgPath);

                        }

                        @Override
                        public void onFailure(int position) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dismissDialog();
                                    ToastUtil.show("图片上传失败");
                                }
                            });
                        }
                    }
            );
        } catch (Exception e) {
            dismissDialog();
            e.printStackTrace();
        }
    }
}
